package com.yc.demo.d1011;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class URLDemo {

    public static void main(String[] args) throws IOException {
        URL url = new URL("http://www.hyycinfo.com:80/index.html?a=100&b=200");
        System.out.println(" 协议：  "+url.getProtocol());
        System.out.println(" 域名：  "+url.getHost());
        System.out.println(" 路径：  "+url.getPath());
        System.out.println(" 参数：  "+url.getQuery());

        // 连接资源
        URLConnection urlConnection = url.openConnection();
        InputStream inputStream = urlConnection.getInputStream();
//        byte[] bytes = new byte[100];
//        int count;
//        while((count = inputStream.read(bytes))>0){
//            System.out.println( new String(bytes, 0, count));
//        }

        // 字節流轉字符流
        try{
            InputStreamReader isr = new InputStreamReader(inputStream,"utf-8");
            char[] chars = new char[100];
            int count;
            while((count=isr.read(chars))>0){
                System.out.print(new String(chars,0,count));
            }
        }finally {
            inputStream.close();
        }


    }

}
